//
//  BrandHomeViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BrandHomeViewController.h"
#import "BrandHomeModel.h"
#import "LineFlowLayout.h"

#define KBrandHomeURL @"http://api.liwushuo.com/v2/brands/20/items?limit=20&offset=0" // 品牌主页

@interface BrandHomeViewController () <UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UITableView *tableView;
// 进入页面刷新的第三方类
@property(nonatomic,strong)MBProgressHUD *HUD;
// 产品数量
@property(nonatomic,strong)NSMutableArray *brandHomeModelArray;
// tableView的表头图片
@property(nonatomic,strong)UIImageView *topView;
// 分区1上添加的集合视图
@property(nonatomic,strong)UICollectionView *collection;

@end

@implementation BrandHomeViewController

#pragma mark - 懒加载 - 
- (NSMutableArray *)brandHomeModelArray
{
    if (!_brandHomeModelArray) {
        _brandHomeModelArray = [NSMutableArray array];
    }
    return _brandHomeModelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"品牌主页";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addTableView];
}

#pragma mark - 在视图控制器上加载tableView -
- (void)addTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kP6Height(170))];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageName] placeholderImage:[UIImage imageNamed:@"placeHolder@2x.jpg"]];
    self.tableView.tableHeaderView = imageView;
    [self.view addSubview:self.tableView];
}

#pragma mark - tableView部分 -
#pragma mark - 返回分区数 - 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - 返回每个分区的组数 - 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - 返回tableView上的cell - 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"brandHomeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    if (indexPath.section == 0) {
        if (self.brandHomeModelArray.count)
        {
            // 设置毛玻璃效果(默认背景图片)
            BrandHomeModel *mod = self.brandHomeModelArray[0];
            UIImageView *imageView = [self addFuzzyEffectImageViewWithFrame:cell.frame model:mod];
            // 在tableView上的cell上添加collectionView
            LineFlowLayout *flowLayout = [[LineFlowLayout alloc]init];
            self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kP6Height(240)) collectionViewLayout:flowLayout];
            self.collection.backgroundView = imageView;
            [self.collection registerClass:[SingleProductCellTwo class] forCellWithReuseIdentifier:@"branddCell"];
            self.collection.delegate = self;
            self.collection.dataSource = self;
            self.collection.showsHorizontalScrollIndicator = NO;
            // 根据滑动collectionView显示的产品,更换模糊效果的背景图片
            flowLayout.lineBlock = ^(NSInteger item){
                UIImageView *imageV = (UIImageView *)self.collection.backgroundView;
                BrandHomeModel *model = self.brandHomeModelArray[item];
                [imageV sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url] placeholderImage:[UIImage imageNamed:@"placeHolder@2x.jpg"]];
            };
            [cell addSubview:self.collection];
        }
    }else
    {
        cell.textLabel.text = @"江南i换肤i";
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kP6width(240);
}

#pragma mark - 返回分区头名称 -
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"热门礼物";
    }
    return @"品牌介绍";
}


#pragma mark - collection部分 - 
#pragma mark - 返回分区数-
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - 返回每个分区cell个数 -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.brandHomeModelArray.count;
}

#pragma mark - 返回集合视图的cell -
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SingleProductCellTwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"branddCell" forIndexPath:indexPath];
    
    [cell.collectBtn addTarget:self action:@selector(collectBtn:) forControlEvents:UIControlEventTouchUpInside];
    BrandHomeModel *model = self.brandHomeModelArray[indexPath.item];
    [cell setBrandHomeItemValueWithModel:model];
    return cell;
}

#pragma mark - 点击cell上的收藏button触发方法 -
- (void)collectBtn:(UIButton *)sender
{
    // 找到点击button所在的cell
    SingleProductCellTwo *cell = (SingleProductCellTwo *)sender.superview.superview;
    // cell对应的indexPath
    NSIndexPath *indexPath = [self.collection indexPathForCell:cell];
    SingleProductModel *model = self.brandHomeModelArray[indexPath.item];
    int number = [cell.collectLabel.text intValue];
    if (!model.isClick) {
        // 改变button的背景图片 收藏数目
        [cell.collectBtn setBackgroundImage:[UIImage imageNamed:@"shoucang2"] forState:UIControlStateNormal];
        model.favorites_count = [NSNumber numberWithInt:number + 1];
        cell.collectLabel.text = model.favorites_count.stringValue;
    }else
    {
        [cell.collectBtn setBackgroundImage:[UIImage imageNamed:@"shoucang1"] forState:UIControlStateNormal];
        model.favorites_count = [NSNumber numberWithInt:number - 1];
        cell.collectLabel.text = model.favorites_count.stringValue;
    }
    model.isClick = !model.isClick;
}


#pragma mark - 界面即将出现时 -
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 界面即将出现时添加刷新
    self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:_HUD];
    self.HUD.labelText = @"正在刷新数据";
    [self.HUD show:YES];
    [self requestBrandHomeData];
}

#pragma mark - 请求品牌主页的数据 -
- (void)requestBrandHomeData
{
    NSRange range = {34,2};
    NSString *url = [KBrandHomeURL stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%@",self.number]];
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dic2 = [dic valueForKey:@"data"];
        NSArray *array = [dic2 valueForKey:@"items"];
        for (NSDictionary *obj in array) {
            BrandHomeModel *model = [BrandHomeModel brandHomeModelWithDictionary:obj];
            [self.brandHomeModelArray addObject:model];
            NSLog(@"%@",model.name);
        }
        [self.HUD removeFromSuperview];
        self.HUD = nil;
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        nil;
    }];
}

#pragma mark - 添加毛玻璃效果 -
- (UIImageView *)addFuzzyEffectImageViewWithFrame:(CGRect)frame model:(BrandHomeModel *)model
{
    // IOS8新特性 模糊效果
    // 创建一个imageView, 它的大小是整个屏幕这么大[UIScreen mainScreen].bounds
    // 给我们的背景图
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url] placeholderImage:[UIImage imageNamed:@"placeHolder@2x.jpg"]];
    imageView.userInteractionEnabled = YES;
    // 初始化一个UIVisualEffectView的对象
    // 我们的模糊效果是覆盖在背景图上面的
    // 所以我们将我们的模糊效果加载到backImage这个ImageView上面
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    // 首先要设置一下我们visualEffectView这个视图的大小
    // 将它设置为backImage一样大
    visualEffectView.frame = imageView.bounds;
    // 然后给他定一个透明度
    visualEffectView.alpha = 0.4;
    // 将visualEffectView添加到imageView上面
    [imageView addSubview:visualEffectView];
    return imageView;
}

@end
